import {
  query as queryUsers,
  employelist,
  employeDetail,
  removeemploye,
  updateEmploye,
  createmploye,
  queryEmploye
} from '../services/employe';

export default {
  namespace: 'employe',

  state: {
    list: [],
    currentEmploye: [],
    currentDetail: {},
  },

  effects: {
    //列表
    *getEmployelist(_, { call, put }) {
      const response = yield call(employelist);
      /* console.log(response); */
      yield put({
        type: 'saveCurrentEmploye',
        payload: response
      });
    },
    *create({ payload: values }, { call, put }) {
      const response = yield call(createmploye, values);
      console.log(response);
      yield put({ type: 'getEmployelist' });
      if (response != '') {
        yield put(routerRedux.push('/employe/employelist'));
      } 
    },
    //删除信息
    *remove({ payload: id }, { call, put }) {
      yield call(removeemploye, id);
      yield put({ type: 'getEmployelist' });
    },
    //查详情信息
    *employedetail({ payload: id }, { call, put }) {
      const response = yield call(employeDetail, id);
      yield put({
        type: 'saveDetailEmploye',
        payload: response,
      });
    },
  },

  reducers: {
    saveCurrentEmploye(state, action) {
      return {
        ...state,
        currentEmploye: action.payload,
      };
    },
    saveDetailEmploye(state, action) {
      return {
        ...state,
        currentDetail: action.payload || {},
      };
    },
    changeNotifyCount(state, action) {
      return {
        ...state,
        currentEmploye: {
          ...state.currentEmploye,
          notifyCount: action.payload,
        },
        currentDetail: {
          ...state.currentDetail,
          notifyCount: action.payload,  
        },
      };
    },
  },
};
